﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Model;
using ZJFramework10.Extension;
using DevExpress.Web.ASPxEditors;

namespace TrainingsApp.IPADApp.Master {
    public partial class ChildrenEdit : PageBase {


        public int UserId {
            get {

                return ZJFramework10.Utility.QueryStringManager.GetValue<int>("uid");
            }
        }

        public int SchoolId {
            get {
                return ZJFramework10.Utility.QueryStringManager.GetValue<int>("sid");
            }
        }

        public int ClassId {
            get {
                return ZJFramework10.Utility.QueryStringManager.GetValue<int>("cid");
            }
        }

        private TS_User_Children_Entity EditEntity;

        private TS_User_Patriarch_Entity EditParentEntity;

        protected void Page_Load(object sender, EventArgs e) {
            if (UserId > 0) {
                EditEntity = TrainingsApp.Logic.TS_User_Children_Logic.TS_User_Children_View(UserId);
                if (EditEntity.Patriarch.HasValue) {
                    EditParentEntity = TrainingsApp.Logic.TS_User_Patriarch_Logic.TS_User_Patriarch_View(EditEntity.Patriarch.Value);
                }
                if (!IsPostBack) {
                    if (EditEntity != null) {
                        this.btnResetPwd.Visible = true;
                        this.txtPassword.ClientVisible = false;
                        this.btnResetPwd.Visible = true;
                        this.txtUserName.Text = EditEntity.UserName;
                        this.txtUserName.ReadOnly = true;
                        if (EditEntity.BirthDay.HasValue)
                            this.txtBirthDay.Date = EditEntity.BirthDay.Value;
                        this.txtIdCard.Text = EditEntity.IdentityCard;
                        this.txtMail.Text = EditEntity.Email;
                        this.txtMobile.Text = EditEntity.Mobile;
                        this.txtName.Text = EditEntity.Name;
                        this.txtPassword.Text = EditEntity.Password;
                        this.rbtnMale.Checked = EditEntity.Gender.HasValue && EditEntity.Gender.Value;
                        this.rbtnFmale.Checked = !this.rbtnMale.Checked;
                        this.txtStuCode.Text = EditEntity.StuCode;
                        this.hidSchoolIdx.Value = EditEntity.SchoolId.Value.ToString();

                        var schoolEntity = TrainingsApp.Logic.TS_School_Logic.TS_School_View(EditEntity.SchoolId.Value);
                        if (schoolEntity != null)
                            this.cbxSchool.Text = schoolEntity.SchoolName;
                        if (EditEntity.ClassesId.HasValue) {
                            var classDataSource = TrainingsApp.Logic.TS_Class_Logic.TS_Class_ListBySchoolId((int)EditEntity.SchoolId.Value);
                            this.cbxClass.DataSource = classDataSource;
                            this.cbxClass.ValueField = "Idx";
                            this.cbxClass.ValueType = typeof(Int64);
                            this.cbxClass.TextField = "DropdownName";
                            this.cbxClass.DataBind();

                            this.cbxClass.Value = EditEntity.ClassesId.Value;
                        }

                        if (EditEntity.Patriarch.HasValue) {
                            EditParentEntity = TrainingsApp.Logic.TS_User_Patriarch_Logic.TS_User_Patriarch_View(EditEntity.Patriarch.Value);

                            this.btnParentReset.Visible = true;
                            this.txtParentPwd.ClientVisible = false;
                            this.btnParentReset.Visible = true;
                            this.txtParentUserName.Text = EditParentEntity.UserName;
                            this.txtParentUserName.ReadOnly = true;
                            if (EditParentEntity.BirthDay.HasValue)
                                this.txtParentBirth.Date = EditParentEntity.BirthDay.Value;
                            this.txtParentIDCard.Text = EditParentEntity.IdentityCard;
                            this.txtParentMail.Text = EditParentEntity.Email;
                            this.txtParentMobile.Text = EditParentEntity.Mobile;
                            this.txtParentName.Text = EditParentEntity.Name;
                            this.txtParentPwd.Text = EditParentEntity.Password;
                            this.rbtnParentMale.Checked = EditParentEntity.Gender.HasValue && EditEntity.Gender.Value;
                            this.rbtnParentFeMale.Checked = !this.rbtnParentMale.Checked;

                            this.txtParentCompany.Text = EditParentEntity.WorkOfCompany;
                            this.txtParentPosition.Text = EditParentEntity.Position;

                            if (EditParentEntity.Refrence.HasValue)
                                this.cbxParentRef.SelectedIndex = EditParentEntity.Refrence.Value;
                        }


                    }
                }
            }
        }



        protected void cbxClass_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e) {
            int schoolId = e.Parameter.AsInt();
            this.cbxClass.DataSource = TrainingsApp.Logic.TS_Class_Logic.TS_Class_ListBySchoolId(schoolId);
            this.cbxClass.ValueField = "Idx";
            this.cbxClass.ValueType = typeof(Int64);
            this.cbxClass.TextField = "DropdownName";
            this.cbxClass.DataBind();
        }


        protected void cbxSchoolList_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e) {
            string temp = e.Parameter;
            if (!string.IsNullOrEmpty(temp)) {
                var cbxSchoolList = (cbxSchool.FindControl("cbxSchoolList") as ASPxListBox);
                cbxSchoolList.DataSource = TrainingsApp.Logic.TS_School_Logic.TS_School_ListToSearch(temp);
                cbxSchoolList.ValueType = typeof(Int64);
                cbxSchoolList.DataBind();
            }
        }


        protected void btnSave_Click(object sender, EventArgs e) {
            if (UserId > 0) {

            }
            else {
                EditEntity = new TS_User_Children_Entity();
                EditEntity.UserName = this.txtUserName.Text;
                EditEntity.Password = this.txtPassword.Text;
                EditParentEntity = new TS_User_Patriarch_Entity();
                EditParentEntity.UserName = this.txtParentUserName.Text;
                EditParentEntity.Password = this.txtParentPwd.Text;
            }
            EditEntity.BirthDay = this.txtBirthDay.Date;
            EditEntity.ClassesId = GetComboValue(this.cbxClass.Value);
            EditEntity.Email = this.txtMail.Text;
            EditEntity.Gender = this.rbtnMale.Checked ? true : false;
            EditEntity.IdentityCard = this.txtIdCard.Text;
            EditEntity.Mobile = this.txtMobile.Text;
            EditEntity.Name = this.txtName.Text;
            EditEntity.PhotoImage = string.Empty;
            EditEntity.SchoolId = this.hidSchoolIdx.Value.AsInt64();
            EditEntity.UserType = (byte)UserType.Children;
            EditEntity.StuCode = this.txtStuCode.Text;
            EditEntity.UserSource = UserSources.Valid;
            EditParentEntity.BirthDay = this.txtBirthDay.Date;
            EditParentEntity.Email = this.txtMail.Text;
            EditParentEntity.Gender = this.rbtnParentMale.Checked ? true : false;
            EditParentEntity.IdentityCard = this.txtIdCard.Text;
            EditParentEntity.Mobile = this.txtMobile.Text;
            EditParentEntity.Name = this.txtName.Text;
            EditParentEntity.PhotoImage = string.Empty;
            EditParentEntity.UserType = (byte)UserType.Parent;
            EditParentEntity.WorkOfCompany = this.txtParentCompany.Text;
            EditParentEntity.Position = this.txtParentPosition.Text;
            EditParentEntity.Refrence = (byte)this.cbxParentRef.SelectedIndex;
            EditParentEntity.UserType = UserSources.Valid;
            if (UserId > 0) {
                TrainingsApp.Logic.TS_User_Children_Logic.TS_User_Children_Update(EditEntity, EditParentEntity);
            }
            else {
                TrainingsApp.Logic.TS_User_Children_Logic.TS_User_Children_Add(EditEntity, EditParentEntity);
            }

            RedirectTo(GetBackUrl(), "保存成功");
        }

        private long? GetComboValue(object val) {
            long temp = val.AsInt64();
            if (temp > 0) return temp;
            else return null;
        }

        private int? GetComboValueToInt(object val) {
            int temp = val.AsInt();
            if (temp > 0) return temp;
            else return null;
        }

        protected void btnBack_Click(object sender, EventArgs e) {
            RedirectTo(GetBackUrl());
        }


        private string GetBackUrl() {

            string url = "ChildrenManager.aspx";
            bool flag = false;
            if (SchoolId > 0) {
                flag = true;
                url += "?sid=" + SchoolId;
            }
            if (ClassId > 0) {
                if (!flag)
                    url += "?cid=" + ClassId;
                else
                    url += "&cid=" + ClassId;
            }
            return url;
        }
    }
}